package com.example.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.example.pojo.ResultBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

@RestController
public class CodeController {

    /**
     * 获取验证码
     * @param session
     * @return
     * @throws IOException
     */
    @GetMapping("/getCode")
    public ResultBean getCode(HttpSession session) throws IOException {
        //定义图形验证码的长、宽、验证码字符数、干扰元素个数
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 20);
        session.setAttribute("code",captcha.getCode());
        String base64Data = captcha.getImageBase64Data();
        return ResultBean.success(base64Data);
    }
}
